<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;
use App\Models\UserGrowing;
use App\Models\UserAttribute;
use App\Models\UserDeleteLog;
use App\User;
class DeleteUserInfoToTest extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'delete:user_info {mobile}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '删除用户信息';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        $mobile = $this->argument('mobile');
        $user = User::where('mobile',$mobile)->first();
//        dd($user);
        DB::beginTransaction();
        try{
            DB::table('users')->where('mobile',$mobile)->delete();
            DB::table('users_growing')->where('user_id',$user->id)->delete();
            DB::table('users_attribute')->where('user_id',$user->id)->delete();
            $userLog = new UserDeleteLog();
            $userLog->mobile = $mobile;
            $userLog->user_id = $user->id;
            $userLog->content = json_encode($user);
            $userLog->save();
            DB::commit();
            $this->info('删除成功');
        }catch(\Exception $e){
            DB::rollback();
            $this->info('删除失败:'.$e->getMessage());
        }

    }
}
